package com.gxuwz.attendance.dao;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.google.gson.Gson;
import com.gxuwz.attendance.entity.Class;
import com.gxuwz.attendance.vo.Page;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

@Repository("ClassDao")
public class ClassDao extends SessionFactoryDao{

	/**
	 * 更新班级信息
	 * @param clazz
	 * @return
	 */
	public boolean update(Class clazz) {
		boolean flag = true;
		Session session = null;
		try {
			session = getSession();
			// 开启事务
			session.beginTransaction();
			session.update(clazz);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * 批量删除班级信息
	 * @param ids
	 * @return
	 */
	public boolean delete(String ids) {
		JSONArray array = JSONArray.fromObject(ids);

		Session session = null;
		session = getSession();
		
		boolean flag = true;
		
		session.beginTransaction();
		try {
			
			for(int i = 0; i < array.size(); i++) {
				Class clazz = new Class();
				
				clazz.setClassNo(array.getJSONObject(i).getString("classNo"));
				
				session.delete(clazz);
			}
			session.getTransaction().commit();
			
		}catch(Exception e) {
			flag = false;
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally {
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * 添加班级信息
	 * @param clazz
	 * @return
	 */
	public boolean add(Class clazz) {
		boolean flag = true;
		
		Session session = null;
		
		long timeMillis = System.currentTimeMillis();
		
		try {
			session = getSession();
			clazz.setClassNo(timeMillis + "");
			
			// 开启事务
			session.beginTransaction();
			session.save(clazz);
			session.getTransaction().commit();
		}catch (Exception e) {
			// 出现异常更新失败
			flag = false;
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			session.close();
		}
		
		return flag;
	}
	
	
	/**
	 * （分页）模糊查询
	 * @param className
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Class> findPage(Class clazz, Page page){
		List<Class> list = new ArrayList<Class>();
		String hql = "";
		if(clazz.getClassName() == null || "".equals(clazz.getClassName())) {
			hql = "from Class where 1=1 ";
		}
		if(clazz.getCollegeNo() != null && !"".equals(clazz.getCollegeNo())) {
			hql = hql + "and collegeNo ='" + clazz.getCollegeNo() + "' ";
			
		}
		if(clazz.getClassName() != null && !"".equals(clazz.getClassName() )){
			hql = hql + "and className like '%" + clazz.getClassName() + "%'";
		}
		
		list = getSession().createQuery(hql).list();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		session.setAttribute("classSize", list.size());
		
		session.setAttribute("clazzList", list);
		
		Gson gson = new Gson();
		session.setAttribute("JsonClass", gson.toJson(list));
		
		if(!list.isEmpty()) {
			// 分页
			if(page.getPage() * page.getRows() > list.size()) {
				list = list.subList(page.getOffset(),list.size());
			}else {
				list = list.subList(page.getOffset(), page.getPage() * page.getRows());
			}
		}
		
		
		return list;
	}
	
	
	/**
	 * 模糊查询
	 * @param className
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Class> findByName(String className){
		List<Class> list = new ArrayList<Class>();
		String hql = "from Class";
		if(className != null && !"".equals(className)) {
			hql = "from Class where className like '%" + className + "%'";
		}
		
		list = getSession().createQuery(hql).list();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		session.setAttribute("clazzList", list);
		
		Gson gson = new Gson();
		
		
		session.setAttribute("JsonClass", gson.toJson(list));
		
		return list;
	}
}
